package info.androidhive.tabsswipe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



import com.example.newscrolling.enity.Movie;
import com.example.newscrolling.service.favouriteDAO;
import com.example.newscrolling.service.UserDAO;

import info.androidhive.tabsswipe.R;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ThisWeekFragment extends Fragment {
	
	
	private ListView listView1;
	private List<Movie> movieList;
	private UserDAO userDao;
	private favouriteDAO fDao;
	private static final String TAG = "ThisWeekFragment";
	private int selectedIndex;// 使用者選擇的列
	private int selectedMode;// 使用者選擇的編輯模式
	private String[] modeList = { "影片介紹", "加入我的最愛" };
	private Movie movie;
	private ArrayList<HashMap> arrayList;
	private int selected;
	
	public ThisWeekFragment(int selected) {
		this.selected = selected;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_this_week, container, false);
		return rootView;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		initView();
		initHandler();
	}

	

	private void initView() {
		// TODO Auto-generated method stub
		UserDAO userDao = new UserDAO(getActivity()); 
			listView1 = (ListView)getActivity().findViewById(R.id.listView1);
			//movieList = userDao.getAll(selected);
			arrayList = userDao.getAllm(selected);
			listView1.setAdapter(new MovieBaseAdapter(getActivity(),arrayList));
			//MovieBaseAdapter adapter = new MovieBaseAdapter(getActivity(), arrayList);
			//listView1.setAdapter(adapter);
	
	
	}
	
	private void initHandler() {
		// TODO Auto-generated method stub
		// ListView列表
		listView1.setOnItemClickListener(new AdapterView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedIndex = position;
				// TODO Auto-generated method stub
				AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
				dialog.setTitle("請選擇");
				dialog.setSingleChoiceItems(modeList, -1,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								selectedMode = which;
							}
						});
				dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String selectedText = modeList[selectedMode];
						Intent it = new Intent();
						Bundle bundle = new Bundle();
						bundle.putInt("M_POSITION", selectedIndex);
						it.putExtras(bundle);
						it.setClass(getActivity(),DetailActivity.class);
						ThisWeekFragment.this.startActivity(it);

						
					}
				});
				dialog.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}

						});
				dialog.show();
			}
			
		
		});

			
	}	
	
}
	
	
	


	